package 数据结构;


import java.util.*;

/**
 * @description:
 * @author: ywk
 * @date: 2021-03-13
 */
public class 设计Twitter {
    private static int timestamp = 0;
    private HashMap<Integer, User> userMap = new HashMap<>();

    public class Tweet {
        private int id;
        private int time;
        private Tweet next;

        public Tweet(int id, int time) {
            this.id = id;
            this.time = time;
            this.next = null;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getTime() {
            return time;
        }

        public void setTime(int time) {
            this.time = time;
        }

        public Tweet getNext() {
            return next;
        }

        public void setNext(Tweet next) {
            this.next = next;
        }
    }

    public class User {
        private int id;
        public Set<Integer> followed;
        //用户发表的推文链表头结点
        public Tweet head;

        public User(int userId) {
            followed = new HashSet<>();
            this.id = userId;
            this.head = null;
            follow(id);
        }

        private void follow(int userId) {
            followed.add(userId);
        }

        public void unfollow(int userId) {
            if (userId != this.id) {
                followed.remove(userId);
            }
        }

        public void post(int tweetId) {
            Tweet twt = new Tweet(tweetId, timestamp);
            timestamp++;
            twt.setNext(head);
            head = twt;
        }
    }

    public void postTweet(int userId, int tweetId) {
        if (!userMap.containsKey(userId)) {
            userMap.put(userId, new User(userId));
        }
        User u = userMap.get(userId);
        u.post(tweetId);
    }

    public void follow(int followerId, int followeeId) {
        if (!userMap.containsKey(followeeId)) {
            User u = new User(followeeId);
            userMap.put(followeeId, u);
        }
        if (!userMap.containsKey(followerId)) {
            User u = new User(followerId);
            userMap.put(followerId, u);
        }
        userMap.get(followerId).follow(followeeId);
    }

    public void unfollow(int followerId, int followeeId) {
        if (userMap.containsKey(followerId)) {
            User flwer = userMap.get(followerId);
            flwer.unfollow(followeeId);
        }
    }

    public List getNewsFeed(int userId) {
        List res = new ArrayList<>();
        if (!userMap.containsKey(userId)) return res;
        Set<Integer> users = userMap.get(userId).followed;
        PriorityQueue<Tweet> pq = new PriorityQueue<>(users.size(), (o1, o2) -> o2.time - o1.time);
        for (Integer id : users) {
            //先将所有链表头节点插⼊优先级队列
            Tweet head = userMap.get(id).head;
            if (head == null) {
                continue;
            }
            pq.add(head);
        }
        while (!pq.isEmpty()) {
            if (res.size() == 10) {
                break;
            }
            // 弹出 time 值最⼤的（最近发表
            Tweet twt = pq.poll();
            res.add(twt.id);
            // 将下⼀篇 Tweet 插⼊进⾏排序
            if (twt.next != null) {
                pq.add(twt.next);
            }
        }
        return res;
    }
}
